package io.renren.modules.email;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.generator.dao.ResumeDataDao;
import io.renren.modules.generator.entity.ResumeDataEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.mail.*;
import javax.mail.internet.MimeMessage;
import java.util.Properties;


@Component
@EnableScheduling
public class ReadInformation {
    @Autowired(required = false)
    ResumeDataDao dao;

    @Scheduled(cron = "0 */1 * * * ?")
    public void getMail() {
        MailImfo info = null;
        ResumeDataEntity resume = new ResumeDataEntity();

        try {
            String host = "pop.qq.com";
            String username = "1633542398@qq.com";
            String password = "inrtvyrigedodccf";

            Properties p = new Properties();
            p.setProperty("mail.pop3.host", "pop.qq.com"); // 按需要更改
            p.setProperty("mail.pop3.port", "995");
            // SSL安全连接参数
            p.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            p.setProperty("mail.pop3.socketFactory.fallback", "true");
            p.setProperty("mail.pop3.socketFactory.port", "995");

            Session session = Session.getDefaultInstance(p, null);
            Store store = session.getStore("pop3");
            store.connect(host, username, password);

            Folder folder = store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            Message message[] = folder.getMessages();
            System.out.println("邮件数量:　" + message.length);

            for (int i = 0; i < message.length; i++) {
                try {
                    info = new MailImfo((MimeMessage) message[i]);

                    if (info.isContainAttach((Part) message[i])) {
                        //保存附件
                        info.saveAttachMent(message[i]);
                        //获取附件路径
                        String path = info.getFilePath();

                        ParseUtil parseUtil = new ParseUtil();
                        //解析路径信息
                        String data = parseUtil.judge(path);
                        //System.out.println(data);

                        //姓名  年龄  性别  学历  籍贯  专业  联系方式  邮箱  工作年限
                        String name =data.substring(data.indexOf("姓名") + 3, data.indexOf("姓名") + 7);
                        resume.setName(name);
                        System.out.println(resume.getName());
                        String age =data.substring(data.indexOf("年龄")+3,data.indexOf("年龄")+7);
                        resume.setAge(age);
                        System.out.println(resume.getAge());
                        String sex =data.substring(data.indexOf("性别")+3,data.indexOf("性别")+5);
                        resume.setSex(sex);
                        System.out.println(resume.getSex());
                        String education =data.substring(data.indexOf("学历")+3,data.indexOf("学历")+6);
                        resume.setEducation(education);
                        System.out.println(resume.getEducation());
                        String nativePlace =data.substring(data.indexOf("籍贯")+3,data.indexOf("籍贯")+10);
                        resume.setNativePlace(nativePlace) ;
                        System.out.println(resume.getNativePlace());
                        String specialty =data.substring(data.indexOf("专业")+3,data.indexOf("专业")+9);
                        resume.setSpecialty(specialty);
                        System.out.println(resume.getSpecialty());
                        String tel =data.substring(data.indexOf("联系方式")+5,data.indexOf("联系方式")+12);
                        resume.setTel(tel);
                        System.out.println(resume.getTel());
                        String email =data.substring(data.indexOf("邮箱")+3,data.indexOf("邮箱")+20);
                        resume.setEmail(email);
                        System.out.println(resume.getEmail());
                        String experiences=data.substring(data.indexOf("工作年限")+5,data.indexOf("工作年限")+10);
                        resume.setExperiences(experiences);
                        System.out.println(resume.getExperiences());
                        QueryWrapper<ResumeDataEntity> objectQueryWrapper = new QueryWrapper<>();
                        objectQueryWrapper.eq("tel",resume.getTel());
                        if(dao.selectCount(objectQueryWrapper)==0){

                            dao.insert(resume);
                        }

                    }
                } catch (NoSuchProviderException e) {
                    e.printStackTrace();
                } catch (MessagingException e) {
                    e.printStackTrace();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }


        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }
}